import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
/**
 * Player actor used by the Emergency game
 * 
 * @author Jonathan van Es
 */
public class person extends Actor
{
    // actor images
    private final GreenfootImage FWwalk1= new GreenfootImage("forward1.png");  
    private final GreenfootImage FWwalk2= new GreenfootImage("forward2.png");  
    private final GreenfootImage FWwalk3= new GreenfootImage("forward3.png"); 
    private final GreenfootImage FWwalk4= new GreenfootImage("forward4.png");  
    private final GreenfootImage FWwalk5= new GreenfootImage("forward5.png");  
    private final GreenfootImage FWwalk6= new GreenfootImage("forward6.png"); 
    private final GreenfootImage FWwalk7= new GreenfootImage("forward7.png");  
    private final GreenfootImage FWwalk8= new GreenfootImage("forward8.png");  
    private final GreenfootImage FWwalk9= new GreenfootImage("forward9.png");
    private final GreenfootImage FWwalk10= new GreenfootImage("forward10.png");
    private final GreenfootImage DWwalk1= new GreenfootImage("down1.png");  
    private final GreenfootImage DWwalk2= new GreenfootImage("down2.png");  
    private final GreenfootImage DWwalk3= new GreenfootImage("down3.png"); 
    private final GreenfootImage DWwalk4= new GreenfootImage("down4.png");  
    private final GreenfootImage DWwalk5= new GreenfootImage("down5.png");  
    private final GreenfootImage DWwalk6= new GreenfootImage("down6.png"); 
    private final GreenfootImage DWwalk7= new GreenfootImage("down7.png");  
    private final GreenfootImage DWwalk8= new GreenfootImage("down8.png");  
    private final GreenfootImage DWwalk9= new GreenfootImage("down9.png");
    private final GreenfootImage DWwalk10= new GreenfootImage("down10.png");
    private final GreenfootImage RIwalk1= new GreenfootImage("right1.png");  
    private final GreenfootImage RIwalk2= new GreenfootImage("right2.png");  
    private final GreenfootImage RIwalk3= new GreenfootImage("right3.png"); 
    private final GreenfootImage RIwalk4= new GreenfootImage("right4.png");  
    private final GreenfootImage RIwalk5= new GreenfootImage("right5.png");  
    private final GreenfootImage RIwalk6= new GreenfootImage("right6.png"); 
    private final GreenfootImage RIwalk7= new GreenfootImage("right7.png");  
    private final GreenfootImage RIwalk8= new GreenfootImage("right8.png");  
    private final GreenfootImage RIwalk9= new GreenfootImage("right9.png");
    private final GreenfootImage RIwalk10= new GreenfootImage("right10.png");
    private final GreenfootImage LEwalk1= new GreenfootImage("left1.png");  
    private final GreenfootImage LEwalk2= new GreenfootImage("left2.png");  
    private final GreenfootImage LEwalk3= new GreenfootImage("left3.png"); 
    private final GreenfootImage LEwalk4= new GreenfootImage("left4.png");  
    private final GreenfootImage LEwalk5= new GreenfootImage("left5.png");  
    private final GreenfootImage LEwalk6= new GreenfootImage("left6.png"); 
    private final GreenfootImage LEwalk7= new GreenfootImage("left7.png");  
    private final GreenfootImage LEwalk8= new GreenfootImage("left8.png");  
    private final GreenfootImage LEwalk9= new GreenfootImage("left9.png");
    private final GreenfootImage LEwalk10= new GreenfootImage("left10.png");

    private double speed = 2;   // current player speed
    private int frame;          // current sequence image of walking player
    private boolean walking;  
    private boolean isKeyPressed; 
    private boolean facingUp;
    private boolean facingRight;     
    private boolean facingLeft;
    private boolean facingDown;
    private Office OfficeWorld;

    
    public person()  
    {  
        setImage(FWwalk1);      // current player direction
        walking = false;  

    }    

    public void act()   
    {  
        Actor actor = getOneIntersectingObject(Desk.class);
        Desk desk;
        if (actor != null)
        {
            desk = (Desk) actor;

        
            if(desk != null && Greenfoot.isKeyDown("up"))
            {   

                desk.getTelephone().setinuse(true);
                if(desk.getCalamiteit() != null && !desk.getCalamiteit().isactief())
                {
                    Office w = (Office) getWorld();
                    w.stop();
                    desk.getCalamiteit().popup();

                }
        
            } 
            if(desk != null && Greenfoot.isKeyDown("down"))
            {

                desk.getTelephone().setinuse(true);
                if(desk.getCalamiteit() != null && !desk.getCalamiteit().isactief())
                {
                    Office w = (Office) getWorld();
                    w.stop();
                    desk.getCalamiteit().popup();

                }

            } 
            if(desk != null && Greenfoot.isKeyDown("left"))
            {

                desk.getTelephone().setinuse(true);
                if(desk.getCalamiteit() != null && !desk.getCalamiteit().isactief())
                {
                    Office w = (Office) getWorld();
                    w.stop();
                    desk.getCalamiteit().popup();

                }

            } 
            if(desk != null && Greenfoot.isKeyDown("right"))
            {

                desk.getTelephone().setinuse(true);
                if(desk.getCalamiteit() != null && !desk.getCalamiteit().isactief())
                {
                    Office w = (Office) getWorld();
                    w.stop();
                    desk.getCalamiteit().popup();

                }

            } 
        }
        checkKeys(); 

    }  

    public void checkKeys()  
    {  
        isKeyPressed = false;  
        if (Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("down"))  
        {  
            stopWalking();  
            isKeyPressed = true;                          
        }  
        else if (Greenfoot.isKeyDown("up"))  
        {  
            walkUp();  
            setLocation (getX(), getY() - 3);  
            isKeyPressed = true;  
        }  
        else if (Greenfoot.isKeyDown("down"))  
        {  
            walkDown();  
            setLocation (getX(), getY() + 3);  
            isKeyPressed = true;  
        }  

        else if (Greenfoot.isKeyDown("right"))  
        {  
            walkRight();  
            setLocation (getX() + 3, getY());  
            isKeyPressed = true;  
        }  
        else if (Greenfoot.isKeyDown("left"))  
        {  
            walkLeft();  
            setLocation (getX() - 3, getY());  
            isKeyPressed = true;  
        }  
        if (!(isKeyPressed))  
        {  
            stopWalking();  
        }  
    }  

    public void walkUp()  
    {  
        walking = true;  
        facingUp = true;  
        frame ++;  
        if(frame==1)  
        {  
            setImage(FWwalk1);  
        }  
        else if(frame==2)  
        {  
            setImage(FWwalk2);  
        }  
        else if(frame==3)  
        {  
            setImage(FWwalk3);  
        }  
        else if (frame==4)  
        {  
            setImage(FWwalk4);  
        }  
        else if(frame==5)  
        {  
            setImage(FWwalk5);  
        }  
        else if(frame==6)  
        {  
            setImage(FWwalk6);  
        }  
        else if(frame==7)  
        {  
            setImage(FWwalk7);  
        }  
        else if (frame==8)  
        {  
            setImage(FWwalk8);  
        }  
        else if(frame==9)  
        {  
            setImage(FWwalk9);  
        }
        else if (frame==10)  
        {  
            setImage(FWwalk10);  
            frame = 1;  
            return;  
        }
    }  

    public void walkDown()  
    {  
        walking = true;  
        facingDown = true;  
        frame ++;  
        if(frame==1)  
        {  
            setImage(DWwalk1);  
        }  
        else if(frame==2)  
        {  
            setImage(DWwalk2);  
        }  
        else if(frame==3)  
        {  
            setImage(DWwalk3);  
        }  
        else if (frame==4)  
        {  
            setImage(DWwalk4);  
        }  
        else if(frame==5)  
        {  
            setImage(DWwalk5);  
        }  
        else if(frame==6)  
        {  
            setImage(DWwalk6);  
        }  
        else if(frame==7)  
        {  
            setImage(DWwalk7);  
        }  
        else if (frame==8)  
        {  
            setImage(DWwalk8);  
        }  
        else if(frame==9)  
        {  
            setImage(DWwalk9);  
        }
        else if (frame==10)  
        {  
            setImage(DWwalk10);  
            frame = 1;  
            return;  
        }
    }  

    public void walkLeft()  
    {  
        walking = true;  
        facingLeft = true;  
        frame ++;  
        if(frame==1)  
        {  
            setImage(LEwalk1);  
        }  
        else if(frame==2)  
        {  
            setImage(LEwalk2);  
        }  
        else if(frame==3)  
        {  
            setImage(LEwalk3);  
        }  
        else if (frame==4)  
        {  
            setImage(LEwalk4);  
        }  
        else if(frame==5)  
        {  
            setImage(LEwalk5);  
        }  
        else if(frame==6)  
        {  
            setImage(LEwalk6);  
        }  
        else if(frame==7)  
        {  
            setImage(LEwalk7);  
        }  
        else if (frame==8)  
        {  
            setImage(LEwalk8);  
        }  
        else if(frame==9)  
        {  
            setImage(LEwalk9);  
        }
        else if (frame==10)  
        {  
            setImage(LEwalk10);  
            frame = 1;  
            return;  
        }
    }  

    public void walkRight()  
    {  
        walking = true;  
        facingRight = true;  
        frame ++;  
        if(frame==1)  
        {  
            setImage(RIwalk1);  
        }  
        else if(frame==2)  
        {  
            setImage(RIwalk2);  
        }  
        else if(frame==3)  
        {  
            setImage(RIwalk3);  
        }  
        else if (frame==4)  
        {  
            setImage(RIwalk4);  
        }  
        else if(frame==5)  
        {  
            setImage(RIwalk5);  
        }  
        else if(frame==6)  
        {  
            setImage(RIwalk6);  
        }  
        else if(frame==7)  
        {  
            setImage(RIwalk7);  
        }  
        else if (frame==8)  
        {  
            setImage(RIwalk8);  
        }  
        else if(frame==9)  
        {  
            setImage(RIwalk9);  
        }
        else if (frame==10)  
        {  
            setImage(RIwalk10);  
            frame = 1;  
            return;  
        }
    }  

    public void stopWalking()  
    {  
        while( walking = false)  {
            if (facingUp = true){
                setImage (FWwalk1); 
            }       
            if(facingLeft = true){
                setImage (LEwalk1);
            }
            if(facingDown = true){
                setImage (DWwalk1);
            }
            if(facingRight = true){
                setImage (RIwalk1);
            }

        }
    }  

}
